package vip.xiaonuo.bi.modular.datasource.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import vip.xiaonuo.bi.modular.datasource.doris.DorisTbService;
import vip.xiaonuo.bi.modular.datasource.entity.DatasourceFormField;
import vip.xiaonuo.bi.modular.datasource.entity.DatasourceInfo;
import vip.xiaonuo.bi.modular.datasource.entity.DatasourceType;
import vip.xiaonuo.bi.modular.datasource.param.*;
import vip.xiaonuo.bi.modular.datasource.service.DatasourceClassifyService;
import vip.xiaonuo.bi.modular.datasource.service.DatasourceFormFieldService;
import vip.xiaonuo.bi.modular.datasource.service.DatasourceInfoService;
import vip.xiaonuo.bi.modular.datasource.service.DatasourceTypeService;
import vip.xiaonuo.common.annotation.CommonLog;
import vip.xiaonuo.common.pojo.CommonResult;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import java.util.List;

/**
 * 数据源分类表控制器
 *
 * @author 顾育司
 * @date  2023/12/15 12:09
 */
@Api(tags = "数据源分类表控制器")
@ApiSupport(author = "SNOWY_TEAM", order = 1)
@RestController
@Validated
public class DatasourceController {

    @Resource
    private DatasourceClassifyService datasourceClassifyService;

    @Autowired
    private DatasourceTypeService datasourceTypeService;

    @Autowired
    private DatasourceFormFieldService datasourceFormFieldService;

    @Resource
    private DatasourceInfoService datasourceInfoService;
    /**
     * 获取数据源分类表类型
     */
    @ApiOperationSupport(order = 1)
    @ApiOperation("获取数据源类型")
//    @SaCheckPermission("/bi/classify/page")
    @GetMapping("/bi/dataSourseClassify/list")
    public CommonResult getlist(DatasourceClassifyPageParam datasourceClassifyPageParam) {
        return CommonResult.data(datasourceClassifyService.getlist(datasourceClassifyPageParam));
    }

    /**
     * 获取数据源类型的具体种类
     */
    @ApiOperationSupport(order = 2)
    @ApiOperation("获取数据源类型的具体种类")
    @GetMapping("/bi/dataSourseType/list")
    public CommonResult dataSourseType(String classifyId) {
        List<DatasourceType> list =  datasourceTypeService.getlist(classifyId,null);
        return CommonResult.data(list);
    }

    /**
     * 获取数据源类型表单配置项
     */
    @ApiOperationSupport(order = 3)
    @ApiOperation("获取数据源类型表单配置项")
    @GetMapping("/bi/dataSourseFormFields/getByType")
    public CommonResult getTypeFormFields(String datasourceName) {
        List<DatasourceFormField> list =  datasourceFormFieldService.getTypeFormFields(datasourceName);
        return CommonResult.data(list);
    }

    /**
     * 测试数据库连接连通性
     */
    @ApiOperationSupport(order = 4)
    @ApiOperation("测试数据库连接连通性")
    @GetMapping("/bi/dataSourse/testConection")
    public CommonResult testConection(DatasourceAddParam datasourceAddParam) {
        return CommonResult.data(datasourceInfoService.testConection(datasourceAddParam));
    }


    /**
     * 获取数据库下得表
     */
    @ApiOperationSupport(order = 4)
    @ApiOperation("获取数据库下得表")
    @GetMapping("/bi/dataSourse/tableList")
    public CommonResult tableList(DatasourceTableListParam datasourceTableListParam) {
        return CommonResult.data(datasourceInfoService.tableList(datasourceTableListParam));
    }

    @ApiOperationSupport(order = 4)
    @ApiOperation("获取数据源表的列结构")
    @GetMapping("/bi/dataSourse/columList")
    public CommonResult columList(DatasourceColumListParam datasourceColumListParam) {
        return CommonResult.data(datasourceInfoService.getcolumList(datasourceColumListParam));
    }

    @ApiOperationSupport(order = 4)
    @ApiOperation("获取数据源表预览数据")
    @GetMapping("/bi/dataSourse/tablePreview")
    public CommonResult tablePreview(DatasourceTablePreviewParam datasourceTableListParam) {
        return CommonResult.data(datasourceInfoService.getPreview(datasourceTableListParam));
    }

    @ApiOperationSupport(order = 5)
    @ApiOperation("sql预览数据")
    @PostMapping("/bi/dataSourse/sqlPreview")
    public CommonResult sqlPreview(@RequestBody DatasourceSqlPreviewParam datasourceSqlPreviewParam) {
        return CommonResult.data(datasourceInfoService.sqlPreview(datasourceSqlPreviewParam));
    }



    /**
     * 获取数据源连接信息分页
     *
     * @author 顾育司
     * @date  2023/12/17 13:14
     */
    @ApiOperationSupport(order = 5)
    @ApiOperation("获取数据源连接信息分页")
//    @SaCheckPermission("/bi/datasource/page")
    @GetMapping("/bi/datasource/page")
    public CommonResult<Page<DatasourceInfo>> page(DatasourceInfoPageParam datasourceInfoPageParam) {
        return CommonResult.data(datasourceInfoService.page(datasourceInfoPageParam));
    }

    /**
     * 添加数据源连接信息
     *
     * @author 顾育司
     * @date  2023/12/17 13:14
     */
    @ApiOperationSupport(order = 6)
    @ApiOperation("添加数据源连接信息")
    @CommonLog("添加数据源连接信息")
    @PostMapping("/bi/datasource/add")
    public CommonResult<String> add(@RequestBody @Valid DatasourceInfoAddParam datasourceInfoAddParam) {
        datasourceInfoService.add(datasourceInfoAddParam);
        return CommonResult.ok();
    }

    /**
     * 编辑数据源连接信息
     *
     * @author 顾育司
     * @date  2023/12/17 13:14
     */
    @ApiOperationSupport(order = 7)
    @ApiOperation("编辑数据源连接信息")
    @CommonLog("编辑数据源连接信息")
    @PostMapping("/bi/datasource/edit")
    public CommonResult<String> edit(@RequestBody @Valid DatasourceInfoEditParam datasourceInfoEditParam) {
        datasourceInfoService.edit(datasourceInfoEditParam);
        return CommonResult.ok();
    }

    /**
     * 删除数据源连接信息
     *
     * @author 顾育司
     * @date  2023/12/17 13:14
     */
    @ApiOperationSupport(order = 8)
    @ApiOperation("删除数据源连接信息")
    @CommonLog("删除数据源连接信息")
    @PostMapping("/bi/datasource/delete")
    public CommonResult<String> delete(@RequestBody @Valid @NotEmpty(message = "集合不能为空")
                                       List<String> ids) {
        datasourceInfoService.delete(ids);
        return CommonResult.ok();
    }

    /**
     * 获取数据源连接信息详情
     *
     * @author 顾育司
     * @date  2023/12/17 13:14
     */
    @ApiOperationSupport(order = 9)
    @ApiOperation("获取数据源连接信息详情")
    @CommonLog("获取数据源连接信息详情")
    @GetMapping("/bi/datasource/detail")
    public CommonResult<DatasourceInfo> detail(String id) {
        return CommonResult.data(datasourceInfoService.detail(id));
    }

    @Autowired
    DorisTbService demoService;


    @ApiOperation("Test-doris")
    @CommonLog("Test-doris")
    @GetMapping("/bi/dataSourse/test")
    public CommonResult test() {
        return CommonResult.data(demoService.exeQuerySql(""));
    }
}